package controller

import (
	"express/global"
	"express/handler/request"
	"express/logic"
	"express/pkg/zaplog"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"net/http"
	"strconv"
)

type GetNumber struct {
	//date     string
	//page     int
	//pageSize int
}

type GetTodayNumber struct {
}

func (i *GetNumber) GetNumber(c *gin.Context) {
	zaplog.InitLogger()
	global.SugarLogger.Infof("123", 1234, 12345)
	//defer global.SugarLogger.Sync()
	//zaplog.SimpleHttpGet("www.sogo.com")
	//zaplog.SimpleHttpGet("http://www.sogo.com")
	//var users []model.Users
	userId, _ := c.Get("UserId")
	users := logic.GetUserAll(userId.(string))

	//log.Print(userId.(string))
	//log.Print(users)
	//api_helpers.RespondJSON(c, 200, "获取数据成功", users)
	c.JSON(http.StatusOK, users)
	//if users != nil {
	//	api_helpers.RespondJSON(c, 404, "未获取到数据", users)
	//} else {
	//	api_helpers.RespondJSON(c, 200, "获取数据成功", users)
	//}
}

func (i *GetNumber) GetTodayNumber(c *gin.Context) {
	//global.SugarLogger.Infof("123", 200, "www.baidu.com")
	var userqu request.UsersRequest
	userId, _ := c.Get("UserId") //huo获取中间件id

	//log.Print(userId.(string))
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
	ar := &request.PageRequest{
		Page:     page,
		PageSize: pageSize,
	}
	if err := c.ShouldBind(&userqu); err != nil {
		c.String(500, fmt.Sprint("请求参数错误"))
		return
	}

	//fmt.Println(userqu)
	//var userId string = "1"
	//date := userqu.Date //获取日期
	day := logic.GetTodayNumberLogic(userId.(string), userqu, ar.Page, ar.PageSize)
	c.JSON(http.StatusOK, day)
	//api_helpers.RespondJSON(c, 200, "获取数据成功", day)
	//api_helpers.RespondJSON(c, 200, "获 取数据成功", day)

}

func (i *GetNumber) SetNumber(c *gin.Context) {
	var setNum request.SetNumberRequest
	userId, _ := c.Get("UserId")
	if err := c.ShouldBind(&setNum); err != nil {
		c.String(500, fmt.Sprint("请求参数错误"))
		return
	}
	number := c.Query("number")
	userIdInt := cast.ToInt(userId)
	c.JSON(http.StatusOK, logic.SetNumberLogic(userIdInt, number))
}
